var express = require('express');
var router = express.Router();
var uuid = require('uuid').v1;



// 创建文件夹
router.post('/folder', async function (req, res, next) {
    var folderId = uuid();

    var parentFolderId = req.body.parentFolderId;
    var title = req.body.title;
    var description = req.body.description;
    var parentFolderIndexArray = req.body.parentFolderIndexArray;

    var userId = req.headers.token;
    var projectId = req.body.projectId;

    parentFolderIndexArray.push(folderId);
    parentFolderIndexArray = parentFolderIndexArray.join(';');

    try {
        await Permission.CPP(userId, projectId);
        query(`INSERT INTO folder SET ?`, {
            folder_id: folderId,
            folder_project_id: projectId,
            folder_parent_id: parentFolderId,
            title: title,
            description: description,
            type: 0,
            disable: 0,
        }, function (qerr, vals, fields) {
            if (qerr) {
                res.status(500);
                res.json({
                    msg: '数据库错误',
                    error: qerr
                });
            } else {
                query(`UPDATE folder SET ? WHERE folder_id = ?`, [
                    { folder_index_array: parentFolderIndexArray },
                    parentFolderId
                ], function (qerr, vals, fields) {
                    if (qerr) {
                        res.status(500);
                        res.json({
                            msg: '数据库错误',
                            error: qerr
                        });
                    } else {
                        res.json({
                            folderId: folderId
                        });
                    };
                });
            };
        });
    } catch (error) {
        next(error)
    };
});


// 更新文件夹信息
router.put('/folder', async function (req, res, next) {
    var folderId = req.body.folderId;
    var title = req.body.title;
    var description = req.body.description;

    var userId = req.headers.token;
    var projectId = req.query.projectId;
    try {
        await Permission.CPP(userId, projectId);
        query(`UPDATE folder SET ? WHERE folder_id = ?`, [
            {
                title: title,
                description: description
            },
            folderId
        ], function (qerr, vals, fields) {
            if (qerr) {
                res.status(500);
                res.json({
                    msg: '数据库错误',
                    error: qerr
                });
            } else {
                res.json({
                    folderId: folderId
                });
            };
        });
    } catch (error) {
        next(error)
    };
});


// 更新文件夹序号
router.put('/folder/order/into', async function (req, res, next) {
    var fromFolder = req.body.fromFolder;
    var moveFolder = req.body.moveFolder;
    var toFolder = req.body.toFolder;
    var fromSql = `UPDATE folder SET folder_index_array = ? WHERE folder_id = ?`;
    var moveSql = `UPDATE folder SET folder_parent_id = ? WHERE folder_id = ?`;
    var toSql = `UPDATE folder SET folder_index_array = ? WHERE folder_id = ?`;

    var userId = req.headers.token;
    var projectId = req.query.projectId;
    try {
        await Permission.CPP(userId, projectId);
        try {
            await asyncQuery(fromSql, [fromFolder.index, fromFolder.id]);
            await asyncQuery(moveSql, [moveFolder.parent, moveFolder.id]);
            await asyncQuery(toSql, [toFolder.index, toFolder.id]);
            res.json({});
        } catch (error) {
            next(error)
        };
    } catch (error) {
        next(error)
    };
});


// 更新文件夹序号
router.put('/folder/order/same', async function (req, res, next) {
    var updateObj = req.body.updateObj;
    var updateSql = `UPDATE folder SET folder_index_array = ? WHERE folder_id = ?`;

    var userId = req.headers.token;
    var projectId = req.query.projectId;
    try {
        await Permission.CPP(userId, projectId);
        await asyncQuery(updateSql, [req.body.index, req.body.id]);
        res.json({});
    } catch (error) {
        next(error)
    };
});


// 删除文件夹
router.delete('/folder', async function (req, res, next) {
    var folderId = req.query.folderId;
    var parentId = req.query.parentId;

    var SqlSelf = `UPDATE folder SET disable = 1 WHERE folder_id = ?;`;
    var Sqlfolder = `UPDATE folder SET folder_parent_id = ? WHERE folder_parent_id = ?;`;
    var SqlApis = `UPDATE project_api SET folder_id = ? WHERE folder_id = ?;`;

    try {
        await Permission.CPP(req.headers.token, req.query.projectId);
        await asyncQuery(SqlSelf, [folderId]);
        await asyncQuery(Sqlfolder, [parentId, folderId]);
        await asyncQuery(SqlApis, [parentId, folderId]);
        res.json({});
    } catch (error) {
        next(error)
    };
});



module.exports = router;